package com.uenpay.im.ui.old.activity.bankcard;

import android.util.Log;

import com.uenpay.im.IMApplication;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 我的银行卡
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class MyBankCardNumInvokeItem extends BaseHttpInvokeItem {
	public class MyBankCardNumInvokeItemResult extends HttpInvokeResult {
		public int status;
		public String msg;
		public ArrayList<BankEntity> arrayList = new ArrayList<>();
	}

	public MyBankCardNumInvokeItemResult getOutPut() {
		return (MyBankCardNumInvokeItemResult) GetResultObject();
	}

	public MyBankCardNumInvokeItem() {
		String url = APIConfiguration.getPayUrl() + "users/" + IMApplication.getInstance().getUcrOpendId() + "/bankcards";
		HashMap<String, String> header = new HashMap<>();
		header.put("app_id", "qiyunxin");
		header.put("Authorization", IMApplication.getInstance().getUcrtoken());
		SetHeaders(header);
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		MyBankCardNumInvokeItemResult result = new MyBankCardNumInvokeItemResult();
		try {
			Log.e("responseBody:", responseBody);
			if (responseBody.indexOf("err_code") > -1) {
				JSONObject jsonObject = new JSONObject(responseBody);
				result.status = jsonObject.optInt("err_code");
				result.msg = jsonObject.optString("err_msg");
				result.arrayList = null;
				return result;
			} else {
				JSONArray jsonArray = new JSONArray(responseBody);
				if (jsonArray != null && jsonArray.length() > 0) {
					for (int i = 0, size = jsonArray.length(); i < size; i++) {
						JSONObject jsonObject = jsonArray.optJSONObject(i);
						if (jsonObject != null) {
							BankEntity mBankEntity = new BankEntity();
							mBankEntity.bank_name = jsonObject.optString("bank_name");
							mBankEntity.holder_name = jsonObject.optString("holder_name ");
							mBankEntity.card_no = jsonObject.optString("card_no");
							mBankEntity.status = jsonObject.optInt("status");
							mBankEntity.id = jsonObject.optInt("id");
							result.arrayList.add(mBankEntity);
						}
					}
				}
			}

		} catch (JSONException e) {
			return result;
		}

		return result;
	}
}
